package org.example.javawebday1.service.impl;


import ch.qos.logback.core.util.StringUtil;
import com.aliyuncs.exceptions.ClientException;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.example.javawebday1.mapper.EmpExprMapper;
import org.example.javawebday1.mapper.EmpMapper;
import org.example.javawebday1.pojo.*;
import org.example.javawebday1.service.EmpsService;
import org.example.javawebday1.utils.JwtUtils;
import org.example.javawebday1.utils.OSSOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.time.LocalDateTime;
import java.util.*;

import static com.fasterxml.jackson.databind.type.LogicalType.Array;
import static com.fasterxml.jackson.databind.type.LogicalType.Collection;

@Service
@Slf4j
public class EmpsServiceImpl implements EmpsService {

    @Autowired
    EmpMapper empMapper;

    @Autowired
    OSSOperation ossOperation;

    @Override
    public Result getEmps(EmpQueryForm empQueryForm) {

//
//        Long total = empMapper.getTotal();
//        int index = (page - 1) * pageSize;
//        PageResult<Emp> pageResult = new PageResult<Emp>(total, empMapper.getEmps());
//        log.info("获取员工列表成功 {}", pageResult);

        PageHelper.startPage(empQueryForm.getPage(), empQueryForm.getPageSize());
        List<Emp> emps = empMapper.getEmps(empQueryForm);
        Page<Emp> p = (Page<Emp>) emps;
        PageResult<Emp> pageResult = new PageResult<>(p.getTotal(), p.getResult());
        return Result.success(pageResult);
    }

    @Transactional
    @Override
    public Result addEmps(Emp emp) {

        emp.setCreateTime(LocalDateTime.now());
        emp.setUpdateTime(LocalDateTime.now());

        empMapper.addEmp(emp);
        System.out.println("emp的id是" + emp.getId());
        if (!CollectionUtils.isEmpty(emp.getExprList())) {
            log.info("添加员工 {} 的exprs", emp.getId());
            emp.getExprList().forEach(expr -> {
                expr.setEmpId(emp.getId());
            });
            empMapper.addEmpExprs(emp);
        }
        return Result.success("添加员工成功");
    }

    @Override
    public Result fileUpload(MultipartFile file) {
        try {
            return Result.success(ossOperation.fileUpload(file));
        }
        catch (com.aliyuncs.exceptions.ClientException e) {
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
    }

    @Transactional
    @Override
    public Result deleteEmps(Integer[] ids) {
        empMapper.deleteEmps(ids);
        empMapper.deleteEmpExprs(ids);
        return Result.success("删除员工成功");
    }

    @Transactional
    @Override
    public Result getEmpById(Integer id) {
        Emp emp = empMapper.getEmpById(id);
        emp.setExprList(empMapper.getEmpExprByEmpId(id));
        return Result.success(emp);
    }

    @Transactional
    @Override
    public Result updateEmp(Emp emp) {
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.updateEmp(emp);
        empMapper.deleteEmpExprs(new Integer[]{emp.getId()});
        empMapper.addEmpExprs(emp);
        return Result.success("更新员工成功");
    }

    @Override
    public Result empLogin(String username, String password) {
        Emp emp = empMapper.getEmpByNameAndPassword(username, password);
        if (emp == null)
            return Result.error("用户名或密码错误");
        Map<String, Object> claims = new HashMap<>();
        claims.put("id",emp.getId());
        claims.put("username",emp.getUsername());
        String token = JwtUtils.generateToken(claims);
        LoginInfo loginInfo = new LoginInfo(emp.getId(),emp.getUsername(),emp.getName(),token);
        return Result.success(loginInfo);
    }

    @Override
    public LoginInfo empLogin2(String username, String password) {
        Emp emp = empMapper.getEmpByNameAndPassword(username, password);
        if (emp == null)
            return new LoginInfo();
        Map<String, Object> claims = new HashMap<>();
        claims.put("id",emp.getId());
        claims.put("username",emp.getUsername());
        String token = JwtUtils.generateToken(claims);
        LoginInfo loginInfo = new LoginInfo(emp.getId(),emp.getUsername(),emp.getName(),token);
        return loginInfo;
    }
}
